//引入样式
require('../../assets/css/normalize.css')
require('../../assets/css/reset.css')
require('../../assets/fonts/iconfont.css');
require('../../assets/css/tabbar.less')
require('../../assets/css/navbar.less')
require('./sports_ridding.less')

//引入模块
const dom = require('../../utils/dom.js')
let axios = require('../../utils/axios.js')
let time = require('../../utils/time.js')

document.addEventListener("DOMContentLoaded", function () {
    //1. 地理定位
    let location = null
    function getLocation () {
        AMap.plugin('AMap.Geolocation', function () {
            var geolocation = new AMap.Geolocation({
                // 是否使用高精度定位，默认：true
                enableHighAccuracy: true,
                // 设置定位超时时间，默认：无穷大
                timeout: 5000,
                // 定位按钮的停靠位置的偏移量
                offset: [10, 20],
                //  定位成功后调整地图视野范围使定位位置及精度范围视野内可见，默认：false
                zoomToAccuracy: true,
                //  定位按钮的排放位置,  RB表示右下
                position: 'RB'
            })

            geolocation.getCurrentPosition(function (status, result) {
                if (status == 'complete') {
                    console.log('定位成功', result)
                    //定位成功后获得的经纬度
                    // location = result.position
                } else {
                    console.log('定位失败')
                }
            });
        });
    }
    getLocation();
    //获取得到的第一定位(经纬度)
    // let p1 = location;

    //定义变量
    let timer = null
    let speed = 0
    let duration = 0
    let calorie = 0
    let meter = 0
    //定义运动函数
    function start () {
        timer = setInterval(function () {
            //获取得到第二个经纬度(运动之后的经纬度)
            // getLocation();
            // let p2 = location
            // if (p1 != p2) {
            //     // 返回 p1 到 p2 间的地面距离，单位：米
            //     var dis = AMap.GeometryUtil.distance(p1, p2);
            //     //经纬度之差测出来的运动距离
            //     //meter+=dis
            // }
            //配速(经纬度之差除以时间)
            //speed=dis/++duration

            //消耗卡路里
            //calorie = 70 * meter * 0.1355

            //运动距离(模拟)
            meter += speed
            dom.get('#meter').textContent = meter / 1000
            //配速(模拟)
            //获取两个整数之间的随机数
            function getRandom (min, max) {
                return Math.floor(Math.random() * (max - min + 1) + min)
            }
            speed = getRandom(3, 7)
            dom.get('#speed').textContent = speed + `米/秒`
            //运动时长
            dom.get('#duration').textContent = time.getHMS(duration++)
            // 消耗卡路里(模拟)
            calorie = 70 * meter * 0.1355;
            dom.get('#calorie').textContent = (calorie / 1000).toFixed(2)


        }, 1000)
    }
    start();

    //点击暂停按钮
    dom.get('#pause').addEventListener('click', function () {
        dom.get('#play').style.display = 'block'
        dom.get('#end').style.display = 'block'
        dom.get('#pause').style.display = 'none'
        clearInterval(timer)
    })
    //点击播放按钮
    dom.get('#play').addEventListener('click', function () {
        dom.get('#play').style.display = 'none'
        dom.get('#end').style.display = 'none'
        dom.get('#pause').style.display = 'block'
        start();
    })
    //点击结束按钮并上传运动数据
    dom.get('#end').addEventListener('click', function () {
        history.back()
        alert('运动结束,下次再来')
        axios.post('/api/exercise/save', {
            type: 1,
            duration,
            calorie,
            meter,
        }).then(res => {
            console.log(res);
        })
    })
})